﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SharpCmd.EasterEggs
{
    internal partial class SnakeForm : Form
    {
        private Snake snake;
        private Bitmap buffer;

        public SnakeForm()
        {
            InitializeComponent();
            NewGame();
        }

        private void NewGame()
        {
            snake = new Snake();
            buffer = new Bitmap(400, 400);
            gameTimer.Start();
        }
    
        private void Snake_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Left:
                    snake.Direction = Snake.DirectionEnum.Left;
                    break;
                case Keys.Right:
                    snake.Direction = Snake.DirectionEnum.Right;
                    break;
                case Keys.Space:
                    snake.Jump();
                    break;
                case Keys.R:
                    NewGame();
                    break;
                case Keys.Oemplus:
                    if (gameTimer.Interval > 20)
                        gameTimer.Interval -= 3;
                    break;
                case Keys.OemMinus:
                    gameTimer.Interval+=3;
                    break;
             }
        }

        private void Snake_KeyUp(object sender, KeyEventArgs e)
        {
            snake.Direction = Snake.DirectionEnum.None;
        }

        private void GameTimer_Tick(object sender, EventArgs e)
        {
            //move snake
            if (!snake.Move())
            {
                //collision
                gameTimer.Stop();
                MessageBox.Show("Boom!", "GAME OVER");
                return;
            }
            labelLength.Text = snake.points.Count.ToString();

            //redraw stuff
            using (Graphics g = Graphics.FromImage(buffer))
            {
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                g.Clear(Color.Black);
                foreach (var point in snake.points)
                {
                    g.FillRectangle(Brushes.Green, point.X - 1, point.Y - 1, 3, 3);
                }
            }
            pictureBox1.Image = buffer;
        }
    }
}
